package com.buka.user.controller;

import com.buka.user.entity.User;
import com.buka.user.service.UserService;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

public class RegisterServlet extends HttpServlet {

    UserService userService = new UserService();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //第一步  从request  取出请求参数
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String userName = req.getParameter("userName");
        String phoneNo = req.getParameter("phoneNo");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");//去到空值  两种情况  排查传递的参数是否为空，排查取值时名字是否正确
        String password = req.getParameter("passwd");
        String passwd1 = req.getParameter("passwd1");
//        String hobby = req.getParameter("hobby");
        String[] hobbies = req.getParameterValues("hobby");

        String hobbyStr = Arrays.stream(hobbies).collect(Collectors.joining(","));

        String addr = req.getParameter("addr");

        //将请求参数封装成对象
        User user = new User();
        user.setUserName(userName);
        user.setPasswd(passwd1);
        user.setPhoneNo(phoneNo);
        user.setEmail(email);
        user.setSex(Integer.parseInt(sex));
        user.setHobby(hobbyStr);
        user.setAreaId(Long.getLong(addr));
        //TODO 存储数据库
        userService.save(user);


        //返回登录页
        resp.sendRedirect("login.jsp");

    }
}
